package com.yzh.usercenter.common;

import com.yzh.usercenter.enums.ErrorCode;
import com.yzh.usercenter.exception.BusinessException;
import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回类
 *
 * @param <T>
 */
@Data
public class BaseResponse<T> implements Serializable {
    private int code;
    private T data;
    private String message;
    private String description;

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.code = 0;
        baseResponse.data = data;
        baseResponse.message = "成功";
        return baseResponse;
    }

    public static BaseResponse fail(ErrorCode errorCode) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(errorCode.getCode());
        baseResponse.setMessage(errorCode.getMsg());
        baseResponse.setDescription(errorCode.getDescription());
        return baseResponse;
    }

    public static BaseResponse fail(ErrorCode errorCode, String description) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(errorCode.getCode());
        baseResponse.setMessage(errorCode.getMsg());
        baseResponse.setDescription(description);
        return baseResponse;
    }

    public static BaseResponse fail(BusinessException e, String description) {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(e.getCode());
        baseResponse.setMessage(e.getMessage());
        baseResponse.setDescription(description);
        return baseResponse;
    }
}
